<?php
/**
 * @author bbernath
 *         Date: 03.11.13
 *         Time: 17:17
 * @version 1.0.4
 */

echo "<h3>&raquo; News</h2>";
$sql = "select n.id, n.topic, n.content as news_content, u.name as user_name, n.written, nc.name as news_category_name,
        u.email as user_email from news n, user u, news_category nc
        where n.user = u.id
        and n.news_category = nc.id
        order by n.id desc limit 5";
if (!$stmt = $db->prepare($sql)) {
    echo $db->error;
}
if (!$stmt->execute()) {
    echo $stmt->error;
}
$stmt->bind_result($id, $topic, $news_content, $user_name, $written, $news_category_name, $user_email);

$news = array();
while ($stmt->fetch()) {
    $news[] = array(
        'id' => $id,
        'topic' => $topic,
        'news_content' => $news_content,
        'user_name' => $user_name,
        'written' => $written,
        'news_category_name' => $news_category_name,
        'user_email' => $user_email
    );
}
$stmt->close();

if (count($news)) {
    foreach ($news as $news_entry) {
        echo "<table border='1' id='news''>";
        echo "<tr><td colspan=2><h3>" . $news_entry['topic'] . "</h3></td></tr>";
        echo "<tr><td colspan=2><br>" . $news_entry['news_content'] . "<br><br></td></tr>";
        $date = new DateTime($news_entry['written']);
        echo "<tr><td>Von <a href='mailto:" . $news_entry['user_email'] . "'>" . $news_entry['user_name'] . "</a> am ". strftime('%A, %d.%B %Y', $date->getTimestamp()) . "</td><td><a href=index.php?action=news&site=comment&newsid=" . $news_entry['id'] . ">Kommentar schreiben</a></td></tr>";
        echo "</table>";
        echo "<table><tr><td><br><br></td></tr></table>";
    }
} else {
    echo "Noch keine News vorhanden.";
}